﻿using System.Collections.Generic;
using System.ServiceModel;

namespace RockBus.DataContracts
{
    [MessageContract(WrapperNamespace = StringConstants.RockBusNamespace)]
    public class AddOrUpdateSubscribersRequest
    {
        private AddOrUpdateSubscribersRequest()
        {
            this.Subscribers = new List<SubscriberInfo>();
        }

        public AddOrUpdateSubscribersRequest(SubscriberInfo subscriber)
            : this()
        {
            this.Subscribers.Add(subscriber);
        }

        public AddOrUpdateSubscribersRequest(IEnumerable<SubscriberInfo> subscribers)
            : this()
        {
            ((List<SubscriberInfo>)this.Subscribers).AddRange(subscribers);
        }

        public AddOrUpdateSubscribersRequest(SubscriberConfiguration subscriberConfiguration)
            : this()
        {
            ((List<SubscriberInfo>)this.Subscribers).AddRange(subscriberConfiguration.EvaluationElementDictionary.Values);
        }

        [MessageBodyMember]
        public ICollection<SubscriberInfo> Subscribers { get; private set; }

        public SubscriberConfiguration CreateSubscriberConfiguration(SubscriberConfiguration toExclude)
        {
            SubscriberConfiguration sc = new SubscriberConfiguration();

            foreach (var si in this.Subscribers)
            {
                if (!toExclude.EvaluationElementDictionary.ContainsKey(si.Id) &&
                    !si.IsStatic)
                {
                    sc.EvaluationElementDictionary.Add(si.Id, si);
                }
            }

            return sc;
        }
    }
}